//
//  TableViewExtension.swift
//  W_BAR_MAIN
//
//  Created by SK2 on 2017/10/13.
//  Copyright © 2017年 LC_WJH. All rights reserved.
//

import Foundation
import SnapKit


var uiview_nodataImageName:String = "nodata";

// MARK: - 虽然扩展的UIView,但主要使用面向对象其实是UITableView,UIcollectionView那些啦,当然,不排除你拿来做其他的
extension UIView {
    
    public static var noDataImageName:String {
        get{
            return uiview_nodataImageName;
        }
        set{
            uiview_nodataImageName = newValue;
        }
    }
    
    public func showNoDataView(_ show:Bool){
        showNoDataView(show,warning:"没有数据");
    }
    
    public func showNoDataView(_ show:Bool,warning:String){
        showNoDataView(show,warning:warning,centerOffset:0);
    }
    
    public func showNoDataView(_ show:Bool,warning:String,centerOffset:CGFloat){
        
        if show {
            
            if let nv =   self.viewWithTag(12306) {
                nv.isHidden = false;
                (nv.viewWithTag(101) as! UILabel).text = warning;
            }else{
                
                let nv =  UIView.init();
                let nimg = UIImageView.init(image: UIImage.init(named: UIView.noDataImageName));
                nimg.contentMode = UIView.ContentMode.center;
                let nt = UILabel.init();
                nt.text = warning;
                nt.textColor = UIColor.gray;
                nt.font = UIFont.systemFont(ofSize: 11);
                nt.tag = 101;
                
                nv.addSubview(nimg);
                nv.addSubview(nt);
                nv.tag = 12306;
                
                self.addSubview(nv);
                
                let table = self;
                
                //添加约束
                nv.snp.makeConstraints({
                    (make) in
                    make.centerX.equalTo(table.snp.centerX);
                    make.centerY.equalTo(table.snp.centerY).offset(centerOffset);
                   // make.center.equalTo(table.snp.center);
                });
                
                nimg.snp.makeConstraints({
                    (make) in
                    make.leading.equalTo(nv.snp.leading);
                    make.trailing.equalTo(nv.snp.trailing);
                    make.top.equalTo(nv.snp.top);
                });
                
                nt.snp.makeConstraints({
                    (make) in
                    make.centerX.equalTo(nv.snp.centerX);
                    make.top.equalTo(nimg.snp.bottom).offset(10);
                    make.bottom.equalTo(nv.snp.bottom);
                })
                
            }
            
        }else{
            
            self.viewWithTag(12306)?.isHidden = true;
            
        }
        
        
        
    }
    
    enum ShakeType {
        case Horizontal
        case Vertical
    }
    
    /**
     UITextField的振动方法
     - parameter time:       振动次数
     - parameter delta:      振动偏移量
     - parameter speed:      动画速度
     - parameter shakeType:  振动类型
     - parameter completion:
     */
    func shakeView(time: Int = 10, delta: CGFloat = 5,speed: TimeInterval = 0.03,shakeType: ShakeType = .Horizontal,completion:(() -> Void)? = nil){
        //根据time的奇偶来确定振动方向
        let direction:CGFloat = (time % 2 == 0) ? 1 : -1
        UIView.animate(withDuration: speed, animations: {
            //根据振动类型选择动画
            self.transform = (shakeType == ShakeType.Horizontal) ? CGAffineTransform(translationX: delta * direction, y: 0) : CGAffineTransform(translationX: 0, y: delta * direction)
        }) { (finished) in
            if time <= 0{
                UIView.animate(withDuration: speed, animations: {
                    self.transform = CGAffineTransform.identity
                }, completion: { (finished) in
                    //如果有回调方法，则实现回调方法
                    if let comp = completion{
                        comp()
                    }
                })
                return
            }
            self.shakeView(time: time - 1, delta: delta, speed: speed, shakeType: shakeType, completion: completion)
        }
    }
    
}

